package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.mapper.SpecificationMapper;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.mapper.TypeTemplateMapper;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.pojo.TypeTemplate;
import com.pinyougou.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service(interfaceName = "com.pinyougou.service.TypeTemplateService")
@Transactional
public class TypeTemplateServiceImpl implements TypeTemplateService {

    @Autowired
    private TypeTemplateMapper typeTemplateMapper;

    /**
     * 保存添加的数据
     *
     * @param typeTemplate
     */
    @Override
    public void save(TypeTemplate typeTemplate) {

        typeTemplateMapper.insertSelective(typeTemplate);
    }

    /**
     * 保存修改数据
     *
     * @param typeTemplate
     */
    @Override
    public void update(TypeTemplate typeTemplate) {
         typeTemplateMapper.updateByPrimaryKeySelective(typeTemplate);
    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id) {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids) {
        //创建示范对象
        Example example = new Example(TypeTemplate.class);
        //创建条件对象
        Example.Criteria criteria = example.createCriteria();
        //添加in条件
        criteria.andIn("id", Arrays.asList(ids));
        //删除添加
        typeTemplateMapper.deleteByExample(example);
    }

    /**
     * 根据主键id查询
     *
     * @param id
     */
    @Override
    public TypeTemplate findOne(Serializable id) {

        return  typeTemplateMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询全部
     */
    @Override
    public List<TypeTemplate> findAll() {
        return null;
    }

    /**
     * 多条件分页查询
     *
     * @param typeTemplate
     * @param page
     * @param rows
     */
    @Override
    public PageResult findByPage(TypeTemplate typeTemplate, int page, int rows) {

        try {
            PageInfo<TypeTemplate> pageInfo = PageHelper.startPage(page,rows).doSelectPageInfo(new ISelect() {
                @Override
                public void doSelect() {
                    typeTemplateMapper.findAll(typeTemplate);
                }
            });
            return new PageResult(pageInfo.getEndRow(),pageInfo.getList());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询模板数据
     */
    @Override
    public List<Map<String, Object>> findTypeTemplateList() {

        try {
            return  typeTemplateMapper.findTypeTemplateList();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据模板的id来查询规格和规格选项的数据
     *
     * @param id
     */
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    
    @Override
    public List<Map> findSpecTempPlateById(Long id) {


        try {
            //首先根据模板id查询模板对象
            TypeTemplate typeTemplate = findOne(id);
            /**
             *
             * [
             *    {"id":27,"text":"网络","options" : [{},{}]},
             *   {"id":32,"text":"机身内存","options" : [{},{}]}
             * ]
             *
             * JSON.parseArray(); // [{},{}]
             * JSON.parseObject(); // {}
             */
            List<Map> spceList = JSON.parseArray(typeTemplate.getSpecIds(), Map.class);

            /** 遍历spceList集合，根据集合中的每一个规格id来查询规格选项数据*/
            for (Map map : spceList) {
                //创建查询条件对象
                SpecificationOption specificationOption =new SpecificationOption();
                specificationOption.setSpecId(Long.valueOf(map.get("id").toString()));

                //根据选项id来查询规格选项表的数据
                //options=[{},{},{}]
                List<SpecificationOption> specOptions = specificationOptionMapper.select(specificationOption);
                //options{ [{},{}]}每遍历一次，向一个map添加
                map.put("options", specOptions);
            }
            return spceList;
        } catch (NumberFormatException e) {
            throw new RuntimeException(e);
        }


    }


}
